package com.example.controller;

import com.example.dto.RawMaterialsConfigDTO;
import com.example.excel.ZyRawMaterialsConfigExcel;
import com.example.service.RawMaterialsConfigService;

import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;
import com.example.utils.ExcelUtils;

/**
* 原材料配置表
*
* @author yyyyjinying 1020066395@qq.com
* @since 1.0-SNAPSHOT 2025-04-29
*/
@RestController
@RequestMapping("zyrawmaterialsconfig")
@Tag(name = "原材料配置表")
public class RawMaterialsConfigController {
    @Autowired
    private RawMaterialsConfigService rawMaterialsConfigService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @RequestMapping(value = "page", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("hasAuthority('sys:zyrawmaterialsconfig:page')")
    public Result<PageData<RawMaterialsConfigDTO>> page(@RequestParam Map<String, Object> params){
        PageData<RawMaterialsConfigDTO> page = rawMaterialsConfigService.page(params);

        return new Result<PageData<RawMaterialsConfigDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:zyrawmaterialsconfig:info')")
    public Result<RawMaterialsConfigDTO> get(@PathVariable("id") Long id){
        RawMaterialsConfigDTO data = rawMaterialsConfigService.get(id);

        return new Result<RawMaterialsConfigDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('sys:zyrawmaterialsconfig:save')")
    public Result<String> save(@RequestBody RawMaterialsConfigDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        rawMaterialsConfigService.save(dto);

        return new Result<String>().ok("ok");
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('sys:zyrawmaterialsconfig:update')")
    public Result<String> update(@RequestBody RawMaterialsConfigDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        rawMaterialsConfigService.update(dto);

        return new Result<String>().ok("ok");
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('sys:zyrawmaterialsconfig:delete')")
    public Result<String> delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        rawMaterialsConfigService.delete(ids);

        return new Result<String>().ok("ok");
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @PreAuthorize("hasAuthority('sys:zyrawmaterialsconfig:export')")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<RawMaterialsConfigDTO> list = rawMaterialsConfigService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "原材料配置表", list, ZyRawMaterialsConfigExcel.class);
    }

}